
$(function() {
	var cache = []
	var edit = null
	var isEdit = false


	function getUser(){
		$.get('http://localhost:3000/api/getUser', function(data) {
			cache = data.data
			var html = '<tr>'
			for (var i = cache.length - 1; i >= 0; i--) {
				html+= '<td>'+cache[i].id+'</td><td>'+cache[i].name+'</td><td>'+cache[i].age+'</td><td>'+cache[i].sex+'</td><td>'+cache[i].phone+'</td><td>'+cache[i].company+'</td><td>'+new Date(cache[i].birthday).Format("yyyy-MM-dd hh:mm:ss")+'</td><td><span class="delete" data-id="'+cache[i].id+'">删除</span><span class="selectUser" data-id="'+cache[i].id+'">编辑</span></td>'
				html+= '</tr>'

			}
			$('tbody').html(html)
		});
	}
	getUser()


	function selectUser(id){
		for (var i = 0; i < cache.length; i++) {
			if (cache[i].id == id) {
				edit = cache[i]
			}
		}

		for (var k in edit) {
			if (k != 'id' && k != 'birthday' && k != 'sex') {
				$('form').find('[name='+k+']').val(edit[k])
			}else if (k == 'sex') {
				var radio = $('form').find('[name=sex]')

				for (var i = 0; i < radio.length; i++) {
					if (radio.eq(i).val() == edit[k]) {
						radio.eq(i).attr("checked",true)
					}else{
						radio.eq(i).attr("checked",false)
					}

				}
			}
		}
		isEdit = true


		$('.title').eq(1).show().siblings('.title').hide()
	}



	function addUser(){
		var data = $('form').serializeObject()
		var url = 'http://localhost:3000/api/addUser'
		if (isEdit) {
			data.id = edit.id
			isEdit = false
			url = 'http://localhost:3000/api/upDateUser'
			$('.title').eq(0).show().siblings('.title').hide()
		}
		$.post(url,data ,function(data) {
			getUser()
			clearForm()
		});
	}




	function deleteUser(id){
		$.post('http://localhost:3000/api/deleteUser',{id:id} ,function(data) {
			getUser()
		});
	}
	function clearForm(){
		var list = $('form [name]')
		for (var i = 0; i < list.length; i++) {
			var name = list.eq(i).attr('name')
			if (name == 'sex') {
				list.eq(i).attr("checked",false)
			}else{
				list.eq(i).val(' ')
			}
		}

	}




	$('button').click(function(){
		addUser()
	});



	$('table').on('click', '.selectUser', function(event) {
		var id = $(this).data().id
		selectUser(id)
	});

	$('table').on('click', '.delete', function(event) {
		var id = $(this).data().id
		deleteUser(id)
	});



})


Date.prototype.Format = function(fmt) {
	var o = {   
		"M+" : this.getMonth()+1,                
		"d+" : this.getDate(),                   
		"h+" : this.getHours(),                 
		"m+" : this.getMinutes(),                
		"s+" : this.getSeconds(),                
		"q+" : Math.floor((this.getMonth()+3)/3), 
		"S"  : this.getMilliseconds()            
	};   
	if(/(y+)/.test(fmt))   
		fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
	for(var k in o)   
		if(new RegExp("("+ k +")").test(fmt))   
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
		return fmt;   
	}  



	$.fn.serializeObject = function() {  
		var o = {};  
		var a = this.serializeArray();  
		$.each(a, function() {  
			if (o[this.name]) {  
				if (!o[this.name].push) {  
					o[this.name] = [ o[this.name] ];  
				}  
				o[this.name].push(this.value || '');  
			} else {  
				o[this.name] = this.value || '';  
			}  
		});  
		return o;  
	}  